package com.beta.mq.redis;

import com.beta.mq.redis.aop.TransFlowAspect;
import com.beta.mq.redis.user.CurrentUserInvoker;
import com.beta.mq.redis.user.impl.DefaultCurrentUserInvoker;
import com.beta.mq.redis.consumer.Consumer;
import com.beta.mq.redis.consumer.impl.RedisBlockingQueueConsumer;
import com.beta.mq.redis.consumer.work.RedisBlockingQueueListener;
import com.beta.mq.redis.producer.Producer;
import com.beta.mq.redis.producer.impl.RedisBlockingQueueProducer;
import com.beta.mq.redis.producer.worker.ProducerAspect;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(name = {"com.beta.mq.redis.enable"}, havingValue = "true", matchIfMissing = false)
@ConditionalOnClass(name = {"org.redisson.api.RedissonClient", "org.aspectj.lang.annotation.Aspect"})
@ComponentScan(basePackageClasses = RedisMqAutoConfiguration.class)
public class RedisMqAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(Consumer.class)
    public RedisBlockingQueueConsumer redisBlockingQueueConsumer(RedissonClient redissonClient, RedisMqProperties properties) {
        return new RedisBlockingQueueConsumer(redissonClient, properties);
    }

    @Bean
    @ConditionalOnMissingBean(RedisBlockingQueueListener.class)
    public RedisBlockingQueueListener redisBlockingQueueListener() {
        return new RedisBlockingQueueListener();
    }

    @Bean
    @ConditionalOnMissingBean(Producer.class)
    public RedisBlockingQueueProducer redisBlockingQueueProducer(RedissonClient redissonClient, RedisMqProperties properties) {
        return new RedisBlockingQueueProducer(redissonClient, properties);
    }

    @Bean
    @ConditionalOnMissingBean(ProducerAspect.class)
    public ProducerAspect producerWorker(RedisBlockingQueueProducer producer) {
        return new ProducerAspect(producer);
    }

    @Bean
    public TransFlowAspect transFlowAspect() {
        return new TransFlowAspect();
    }

    @Bean
    @ConditionalOnMissingBean(CurrentUserInvoker.class)
    public CurrentUserInvoker currentUserInvoker(){
        return new DefaultCurrentUserInvoker();
    }

}
